package CommentTable;

import PageClassTable.AddPageClass;
import PageClassTable.DeletePageClass;
import PageClassTable.QueryPageClass;
import PageClassTable.UpdataPageClass;
import comment.Comment;
import page.PageClass;
import utils.JdbcUtils;

import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.sql.Timestamp;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

public class CommentTable extends Box{
    public CommentTable(int power, String name) {
        super(BoxLayout.Y_AXIS);
        //创建按钮
        JPanel btnPanel =new JPanel();
        btnPanel.setMaximumSize(new Dimension(800,20));
        JButton addBtn=new JButton("添加");
        JButton updateBtn=new JButton("修改");
        JButton deleteBtn=new JButton("删除");
        JButton queryBtn=new JButton("查询");
        btnPanel.add(addBtn);
        btnPanel.add(updateBtn);
        btnPanel.add(deleteBtn);
        btnPanel.add(queryBtn);
        this.add(btnPanel);
        addBtn.addActionListener(new AbstractAction() {
            @Override
            public void actionPerformed(ActionEvent e) {
                AddComment.init(name);
            }
        });
        updateBtn.addActionListener(new AbstractAction() {
            @Override
            public void actionPerformed(ActionEvent e) {
                UpdataComment.init(name);
            }
        });
        deleteBtn.addActionListener(new AbstractAction() {
            @Override
            public void actionPerformed(ActionEvent e) {
                DeleteComment.init(name);
            }
        });
        queryBtn.addActionListener(new AbstractAction() {
            @Override
            public void actionPerformed(ActionEvent e) {
                QueryComment.init();
            }
        });
        String[] ts={"评论编号","评论人","评论文章","评论内容","评论同意数","评论反对数","评论创建时间","评论修改时间"};
        String[][] begin =data_update();
        JTable table =new JTable();
        table.setEnabled(false);
        DefaultTableModel model =new DefaultTableModel(begin,ts); // 新建一个默认数据模型
        JScrollPane scrollPane=new JScrollPane(table);
        table.setModel(model);
        add(scrollPane);
    }
    public String[][] data_update(){
        int MX= Comment.sum;
        String[][] data =new String[MX][8];
        int sum=0;
        List<Map<String, Object>> datas= JdbcUtils.JdbcQuery("SELECT * FROM comment");
        for(Map<String,Object> map : datas){
            int id = 0;
            String text = null;
            LocalDateTime timebe=null;
            LocalDateTime timeen=null;
            int agree=0;
            int disagree=0;
            String name=null;
            String user=null;
            for (String key : map.keySet()) {
                if (key.equals("comment_id")) {
                    id = (int) map.get(key);
                }
                if (key.equals("comment_text")) {
                    text = (String) map.get(key);
                }
                if (key.equals("comment_releasedata")) {
                    Object value = map.get(key);
                    if (value instanceof Timestamp) {
                        timebe = ((Timestamp) value).toLocalDateTime();
                    } else if (value instanceof LocalDateTime) {
                        timebe = (LocalDateTime) value;
                    }
                }
                if (key.equals("comment_update")) {
                    Object value = map.get(key);
                    if (value instanceof Timestamp) {
                        timeen = ((Timestamp) value).toLocalDateTime();
                    } else if (value instanceof LocalDateTime) {
                        timeen = (LocalDateTime) value;
                    }
                }
                if (key.equals("comment_agreesum")) {
                    agree = (int) map.get(key);
                }
                if (key.equals("comment_againstsum")) {
                    disagree = (int) map.get(key);
                }
                if (key.equals("comment_user")) {
                    user = (String) map.get(key);
                }
                if (key.equals("comment_name")) {
                    name = (String) map.get(key);
                }
            }
            data[sum][0]= String.valueOf(id);
            data[sum][1]= String.valueOf(user);
            data[sum][2]= String.valueOf(name);
            data[sum][3]= String.valueOf(text);
            data[sum][4]= String.valueOf(agree);
            data[sum][5]= String.valueOf(disagree);
            data[sum][6]= String.valueOf(timebe);
            data[sum][7]= String.valueOf(timeen);
            sum++;
        }
        return data;
    }
}
